package com.oracle.controller;

import com.oracle.pojo.News;
import com.oracle.pojo.NewsType;
import com.oracle.service.NewsService;
import com.oracle.service.NewsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.time.LocalDateTime;
import java.util.List;

@Controller
public class NewsController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private NewsTypeService newsTypeService;

    @RequestMapping("/")
    public String getAllNews(Model model){
        List<News> newsList = newsService.getAllNews();
        model.addAttribute("newsList",newsList);
        return "index";
    }

    @RequestMapping(value = "/to/addNews",method = RequestMethod.GET)
    public String toAddNews(Model model){
        List<NewsType> newsTypeList = newsTypeService.getAllNewsType();
        model.addAttribute("newsTypeList",newsTypeList);
        return "news/news_add";
    }

    @RequestMapping(value = "/addNews",method = RequestMethod.POST)
    public String addNews(News news){
        news.setCreateTime(LocalDateTime.now());
        newsService.addNews(news);
        return "redirect:/newsMgr";
    }

    @RequestMapping(value = "/checkNews/{id}",method = RequestMethod.GET)
    public String checkNews(@PathVariable Integer id,Model model){
        News news = newsService.getNewsById(id);
        model.addAttribute("news",news);
        return "news/news_check";
    }
}
